package com.example.mediademo.service;

import com.example.mediademo.dto.MediaDTO;
import com.example.mediademo.dto.MediaTypeDTO;
import com.example.mediademo.entity.Media;
import com.example.mediademo.entity.MediaType;
import com.example.mediademo.repository.MediaRepository;
import com.example.mediademo.repository.MediaTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MediaService {
    @Autowired
    private MediaRepository mediaRepository;

    @Autowired
    private MediaTypeRepository mediaTypeRepository;

    // 根据单个编号查询
    public MediaDTO getMediaDetails(String keynumber) {
        Map<String, Object> result = mediaRepository.findMediaDetails(keynumber);
        if (result != null && result.get("city") != null && result.get("keynumber") != null && result.get("typeName") != null) {
            return new MediaDTO(
                String.valueOf(result.get("city")),
                String.valueOf(result.get("keynumber")),
                String.valueOf(result.get("typeName"))
            );
        }
        return null;
    }

    // 根据多个编号查询
    public List<MediaDTO> getMediaDetailsByKeynumbers(String keynumbers) {
        if (keynumbers == null || keynumbers.trim().isEmpty()) {
            return List.of();  // 返回空列表而不是null
        }
        
        return Arrays.stream(keynumbers.split(","))
            .map(String::trim)
            .map(this::getMediaDetails)
            .filter(dto -> dto != null && 
                         dto.getCity() != null && 
                         dto.getKeynumber() != null && 
                         dto.getTypeName() != null)
            .collect(Collectors.toList());
    }

    // 根据类型查询并分页
    public Page<Media> findByType(int type, Pageable pageable) {
        return mediaRepository.findByType(type, pageable);
    }

    // 获取所有媒体类型
    public List<MediaTypeDTO> getAllMediaTypes() {
        return mediaTypeRepository.findAll().stream()
            .map(type -> new MediaTypeDTO(type.getId(), type.getName()))
            .collect(Collectors.toList());
    }

    // 分页查询所有媒体
    public Page<MediaDTO> findAllMedia(Pageable pageable) {
        Page<Map<String, Object>> results = mediaRepository.findAllMediaDetails(pageable);
        return results.map(result -> new MediaDTO(
            String.valueOf(result.get("city")),
            String.valueOf(result.get("keynumber")),
            String.valueOf(result.get("typeName"))
        ));
    }

    // 条件查询（支持多个编号）
    public Page<MediaDTO> findByConditions(Integer type, String keynumbers, Pageable pageable) {
        List<String> keynumberList = null;
        if (keynumbers != null && !keynumbers.trim().isEmpty()) {
            keynumberList = Arrays.stream(keynumbers.split(","))
                .map(String::trim)
                .filter(k -> !k.isEmpty())
                .collect(Collectors.toList());
        }
        
        Page<Map<String, Object>> results = mediaRepository.findMediaByConditions(type, keynumberList, pageable);
        return results.map(result -> new MediaDTO(
            String.valueOf(result.get("city")),
            String.valueOf(result.get("keynumber")),
            String.valueOf(result.get("typeName"))
        ));
    }
}
